package com.scada.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scada.dao.DictionaryDao;
import com.scada.inf.DictionaryVisitor;
import com.scada.model.bean.Dictionary;
import com.scada.model.result.ActionResult;
import com.scada.model.result.PageInfo;



@Service("dictionaryVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class DictionaryVisitorImpl implements DictionaryVisitor {
	
	@Autowired
	private DictionaryDao dictionaryDao;

	@Override
	public ActionResult deleteByPrimaryKey(Map<String, Object> map) {
		ActionResult result = new ActionResult();
		List<String> msgList=new ArrayList<String>();
		int i = dictionaryDao.deleteByPrimaryKey(map);
		if(i > 0 ){
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			msgList.add("删除失败");
			result.setMessages(msgList);
		}
		return result;
	}

	@Override
	public ActionResult insertSelective(Dictionary record) {
		ActionResult result = new ActionResult();
		List<String> msgList=new ArrayList<String>();
		if(dictionaryDao.selectByType(record)!=null||dictionaryDao.selectByName(record)!=null){
			result.setSuccess(false);
			if(dictionaryDao.selectByType(record)!=null){
				msgList.add("编码已存在");
			}			
			if(dictionaryDao.selectByName(record)!=null){
				msgList.add("名称已存在");
			}
			result.setMessages(msgList);
			return result;
		}
		int i = dictionaryDao.insertSelective(record);
		if(i > 0 ){
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			msgList.add("新增失败");
			result.setMessages(msgList);
		}
		return result;
	}

	@Override
	public Dictionary selectByPrimaryKey(Map<String, Object> map) {
		
		return dictionaryDao.selectByPrimaryKey(map);
	}

	@Override
	public ActionResult updateByPrimaryKeySelective(Dictionary record) {
		ActionResult result = new ActionResult();
		List<String> msgList=new ArrayList<String>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", 0);
		map.put("id", record.getId());
		Dictionary d =dictionaryDao.selectByPrimaryKey(map);
		if(dictionaryDao.selectByType(record)!=null || dictionaryDao.selectByName(record)!=null){	
			result.setSuccess(false);
			if(dictionaryDao.selectByType(record)!=null&&!record.getType().equals(d.getType())){
				msgList.add("编码已存在");
			}			
			if(dictionaryDao.selectByName(record)!=null&&!d.getKeyName().equals(record.getKeyName())){
				msgList.add("名称已存在");
			}
			result.setMessages(msgList);
			return result;
		}
		try {
			dictionaryDao.updateByPrimaryKeySelective(record);
			result.setSuccess(true);
		}catch (Exception e) {
			result.setSuccess(false);
			msgList.add("更新失败");
			result.setMessages(msgList);
		}
		
		return result;
	}

	@Override
	public PageInfo<Dictionary> selectAll(PageInfo<Dictionary> pageInfo) {
		Dictionary dictionary =(Dictionary) pageInfo.getCondition();
		int count =dictionaryDao.count(dictionary);
		pageInfo.setRecordsTotal(count);
		List<Dictionary> list = dictionaryDao.selectAll(pageInfo);
		pageInfo.setData(list);
		return pageInfo;
	}

	@Override
	public List<Dictionary> list(Dictionary record) {
		try {
			if(dictionaryDao.list(record)!=null){
				return dictionaryDao.list(record);
			}
		} catch (Exception e) {
			throw new RuntimeException("查询失败");
		}
		
		return null;
	}

	@Override
	public int selectIsSysByType(String type) {
	
		return dictionaryDao.selectIsSysByType(type);
	}	

}
